// this file aggregates the kid-parent-wave file to the parent-wave level

clear
clear matrix
clear mata


set maxvar 30000

// run the file construction script
//  run "code/file-construction/kid_parent_wave_file_construction.do"
use data/constructed_data/kid_parent_wave_all.dta, clear

replace khelpr = 0 if mi(khelpr) // if not in helper file they're not helpers!

// is there a better variable for this?
/* Generate transfer by kid groups */
gen kis_female = kgender == 2 if !mi(kgender)
drop kis_male
gen kis_male = kgender == 1 if !mi(kgender)

foreach v of varlist real_ktcamt real_kfcamt khelpr khlphrs  {
	foreach g of varlist kis_male kis_female khelpr klvtenmi {
		gen `g'_`v' = `g'*`v' if !mi(`g') & !mi(`v')
	}

}

drop real_ktcamt_pos real_kfcamt_pos
gen real_ktcamt_pos = real_ktcamt if real_ktcamt > 0 & !mi(real_ktcamt)
gen real_kfcamt_pos = real_kfcamt if real_kfcamt > 0 & !mi(real_kfcamt)

/* Drop the khelpr_khelpr and khlpr_khlphrs */
drop khelpr_khelpr khelpr_khlphrs

/* Collapse to respondent level */
// Keep first nonmissing for respondent level variables taht should stay the same
// Total up transfers
// Count non-missings for all total variables and put n in front of name
loc sum_vars real_kfcamt kis_male_real_kfcamt kis_female_real_kfcamt khelpr_real_kfcamt real_kfcamt_pos real_ktcamt kis_male_real_ktcamt kis_female_real_ktcamt khelpr_real_ktcamt real_ktcamt_pos khelpr kis_male_khelpr kis_female_khelpr khlphrs
loc max_vars hhid inw rwthh ragey_e raracem 
describe `sum_vars'
describe `max_vars'

/* Collapse to respondent wave level */
collapse (firstnm) `max_vars' ///
    (max) any_coresid = coresid ///
    (sum) `sum_vars' ///
    (count) n_klvtenmi = klvtenmi n_real_kfcamt = real_kfcamt	n_kis_male_real_kfcamt = kis_male_real_kfcamt	n_kis_female_real_kfcamt = kis_female_real_kfcamt	n_khelpr_real_kfcamt = khelpr_real_kfcamt	n_klvtenmi_real_kfcamt = klvtenmi_real_kfcamt	n_real_kfcamt_pos = real_kfcamt_pos	n_real_ktcamt = real_ktcamt	n_kis_male_real_ktcamt = kis_male_real_ktcamt	n_kis_female_real_ktcamt = kis_female_real_ktcamt	n_khelpr_real_ktcamt = khelpr_real_ktcamt	n_klvtenmi_real_ktcamt = klvtenmi_real_ktcamt	n_real_ktcamt_pos = real_ktcamt_pos	n_khelpr = khelpr	n_kis_male_khelpr = kis_male_khelpr	n_kis_female_khelpr = kis_female_khelpr	n_khlphrs = khlphrs	n_kis_male_khlphrs = kis_male_khlphrs	n_kis_female_khlphrs = kis_female_khlphrs, by(hhidpn wave)

// Replace sum vars with missing if no non-missing observations (o/w they're 0)
foreach v in  `sum_vars' {
	replace `v' = . if n_`v' == 0
}

/* any transfer indicators */
loc transfer_vars real_ktcamt real_kfcamt khelpr khlphrs 
// Transfer dummies (any vars)
foreach v in `transfer_vars' {
	gen any_`v' = `v' > 0 if !mi(`v')
}

/* Share of transfers going to different groups */
foreach v in `transfer_vars'{
	foreach g in kis_male kis_female khelpr klvtenmi {
		capture gen frac_`g'_`v' = `g'_`v' / `v' if !(mi(`g'_`v') | mi(`v'))
	}
}



// Oldest age of respondent in HH
egen oldest_age_hh = max(ragey_e), by(hhid wave)

// Whether any nonwhite respondents in HH
gen ris_nonwhite = cond(raracem != 1, 1, 0)
replace ris_nonwhite = . if mi(raracem)
egen any_nonwhite_hh = max(ris_nonwhite), by(hhid wave)

gen any_helpr = any_khelpr
gen pos_hlphrs = khlphrs if khlphrs > 0 & !mi(khlphrs)

/* Apply sample restrictions and rename variables to be shorter */
keep if inw == 1 
keep if wave >=2
keep if oldest_age>=50 & oldest_age<=85

drop if any_nonwhite == . 
xtset hhidpn wave
bys hhidpn: egen hhwaves = sum(wave!=.)
tab hhwaves
bysort hhidpn: egen weight = mean(rwthh)
drop if weight==0  /*59 obs*/

su real_k* any_real_*
rename real_ktcamt amt_pk
rename real_ktcamt_pos amt_pk_pos 
rename real_kfcamt amt_kp
rename real_kfcamt_pos amt_kp_pos 
rename any_real_ktcamt at_pk
rename any_real_kfcamt at_kp
rename pos_hlphrs help_hrs_pos // keep
rename khlphrs help_hrs
rename frac_kis_female_real_ktcamt frac_kf_amtpk
rename frac_kis_female_real_kfcamt frac_kf_amtkp
rename frac_kis_female_khelpr frac_kf_helpr 
rename frac_khelpr_real_ktcamt frac_kh_amtpk
rename frac_khelpr_real_kfcamt frac_kh_amtkp

/* require that they have non missing transfer variables */
drop if mi(at_pk) | mi(amt_pk) | mi(at_kp) | mi(amt_kp) | mi(any_helpr)
cap keep hhidpn wave help_hrs any_helpr help_hrs_pos amt_kp amt_kp_pos amt_pk amt_pk_pos at_pk at_kp frac_kh_amtpk
save data/constructed_data/transfers.dta, replace


